package com.zeratul.common.satoken.advice;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import com.zeratul.common.core.result.R;
import com.zeratul.common.core.result.codes.ResultCodes;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * SaToken异常处理器
 */
@Slf4j
@RestControllerAdvice
public class SaTokenExceptionHandler {

    /**
     * 权限码异常
     */
    @ExceptionHandler(NotPermissionException.class)
    public R<String> handleNotPermissionException(NotPermissionException e) {
        return R.fail(ResultCodes.AUTHORIZED_ERROR, e.getMessage());
    }

    /**
     * 角色权限异常
     */
    @ExceptionHandler(NotRoleException.class)
    public R<String> handleNotRoleException(NotRoleException e) {
        return R.fail(ResultCodes.AUTHORIZED_ERROR, e.getMessage());
    }

    /**
     * 认证失败
     */
    @ExceptionHandler(NotLoginException.class)
    public R<String> handleNotLoginException(NotLoginException e) {
        return R.fail(ResultCodes.AUTH_ERROR, e.getMessage());
    }

}
